package com.xrz.sxm.aj.activity;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.xrz.sxm.aj.base.BaseAct;
import com.xrz.sxm.aj.entity.BingDetail;
import com.xrz.sxm.aj.entity.XueweiEntity;
import com.xrz.sxm.aj.entity_tab.DBHelps;
import com.xrz.sxm.aj.listener.CloseAnimationListener;
import com.xrz.sxm.aj.utils.Utils;
import com.xrz.sxm.aj.view.TableView;

import java.util.List;
import java.util.Map;

public class Act_Detail extends BaseAct implements OnClickListener {
	private static final String CLOSE = "close";
	private static final String OPEN = "open";

	private DBHelps m_DbHelps;

	// /////////////////////////概述///////////////////////////
	/** 概述的总界面 */
	@ViewInject(R.id.llyt_gaishu)
	private LinearLayout llyt_gaishu;
	/** 概述前面的加减号 */
	@ViewInject(R.id.gaishu_state)
	private TextView gaishu_state;
	/** 概述的内容 */
	@ViewInject(R.id.gaishu_content)
	private TextView gaishu_content;
	/** 概述操控的view */
	@ViewInject(R.id.llyt_gaishu_click)
	private LinearLayout llyt_gaishu_click;
	// /////////////////////////病因//////////////////////////////
	/** 病因的总界面 */
	@ViewInject(R.id.llyt_bingyin)
	private LinearLayout llyt_bingyin;
	/** 病因前面的加减号 */
	@ViewInject(R.id.bingyin_state)
	private TextView bingyin_state;
	/** 病因的内容 */
	@ViewInject(R.id.bingyin_content)
	private TextView bingyin_content;
	/** 病因操控的view */
	@ViewInject(R.id.llyt_bingyin_click)
	private LinearLayout llyt_bingyin_click;
	// /////////////////////////分析////////////////////////////
	/** 分析的总界面 */
	@ViewInject(R.id.llyt_fenxi)
	private LinearLayout llyt_fenxi;
	/** 分析前面的加减号 */
	@ViewInject(R.id.fenxi_state)
	private TextView fenxi_state;
	/** 分析的内容 */
	@ViewInject(R.id.fenxi_content)
	private TextView fenxi_content;
	/** 分析操控的view */
	@ViewInject(R.id.llyt_fenxi_click)
	private LinearLayout llyt_fenxi_click;
	// /////////////////////////案例////////////////////////////
	/** 案例的总界面 */
	@ViewInject(R.id.llyt_anli)
	private LinearLayout llyt_anli;
	/** 案例前面的加减号 */
	@ViewInject(R.id.anli_state)
	private TextView anli_state;
	/** 案例的内容 */
	@ViewInject(R.id.anli_content)
	private TextView anli_content;
	/** 案例操控的view */
	@ViewInject(R.id.llyt_anli_click)
	private LinearLayout llyt_anli_click;

	// /////////////////////////体会////////////////////////////
	/** 体会的总界面 */
	@ViewInject(R.id.llyt_tihui)
	private LinearLayout llyt_tihui;
	/** 体会前面的加减号 */
	@ViewInject(R.id.tihui_state)
	private TextView tihui_state;
	/** 体会的内容 */
	@ViewInject(R.id.tihui_content)
	private TextView tihui_content;
	/** 体会操控的view */
	@ViewInject(R.id.llyt_tihui_click)
	private LinearLayout llyt_tihui_click;
	// /////////////////////////取穴////////////////////////////
	/** 取穴的总界面 */
	@ViewInject(R.id.llyt_quxue)
	private LinearLayout llyt_quxue;
	/** 取穴前面的加减号 */
	@ViewInject(R.id.quxue_state)
	private TextView quxue_state;
	/** 取穴的内容 */
	@ViewInject(R.id.quxue_content)
	private TableView quxue_content;
	/** 取穴操控的view */
	@ViewInject(R.id.llyt_quxue_click)
	private LinearLayout llyt_quxue_click;

	private Animation m_Animation_open, m_Animation_close;
	/** 从上层穿过来的病名 */
	private String m_Bingming;
	private Map<Integer, List<XueweiEntity>> m_Xueweis;
	private BingDetail m_BingDetail;

	@Override
	protected int setView() {
		return R.layout.layout_detail;
	}

	@Override
	protected void init() {
		m_DbHelps= new DBHelps(ctx);
		m_BingDetail = m_DbHelps.getDetailByBing(m_Bingming);
		m_Xueweis = m_BingDetail.quxues;
		if(m_Xueweis == null){
			toastLong("暂无信息");
			return;
		}
		quxue_content.setXueweis(m_Xueweis);
		llyt_gaishu_click.setOnClickListener(this);
		llyt_bingyin_click.setOnClickListener(this);
		llyt_fenxi_click.setOnClickListener(this);
		llyt_anli_click.setOnClickListener(this);
		llyt_tihui_click.setOnClickListener(this);
		llyt_quxue_click.setOnClickListener(this);
		m_Animation_open = AnimationUtils.loadAnimation(ctx, R.anim.scale_open);
		m_Animation_close = AnimationUtils.loadAnimation(ctx,
				R.anim.scales_close);

		if (m_BingDetail.gs == null || m_BingDetail.gs.equals("")) {
			llyt_gaishu.setVisibility(View.GONE);
		} else {
			gaishu_content.setText(Utils.replaceChar(m_BingDetail.gs));
			llyt_gaishu.setVisibility(View.VISIBLE);
		}

		if (m_BingDetail.byzz == null || m_BingDetail.byzz.equals("")) {
			llyt_bingyin.setVisibility(View.GONE);
		} else {
			bingyin_content.setText(Utils.replaceChar(m_BingDetail.byzz));
			llyt_bingyin.setVisibility(View.VISIBLE);
		}

		if (m_BingDetail.byfx == null || m_BingDetail.byfx.equals("")) {
			llyt_fenxi.setVisibility(View.GONE);
		} else {
			fenxi_content.setText(Utils.replaceChar(m_BingDetail.byfx));
			llyt_fenxi.setVisibility(View.VISIBLE);
		}
		llyt_anli.setVisibility(View.GONE);
		if (m_BingDetail.ajth == null || m_BingDetail.ajth.equals("")) {
			llyt_tihui.setVisibility(View.GONE);
		} else {
			tihui_content.setText(Utils.replaceChar(m_BingDetail.ajth));
			llyt_tihui.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected String setTopTitle() {
		m_Bingming = getIntent().getStringExtra("data");
		return m_Bingming;
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
			case R.id.llyt_gaishu_click:
				changeState(arg0, gaishu_content, gaishu_state);
				break;
			case R.id.llyt_bingyin_click:
				changeState(arg0, bingyin_content, bingyin_state);
				break;
			case R.id.llyt_fenxi_click:
				changeState(arg0, fenxi_content, fenxi_state);
				break;
			case R.id.llyt_anli_click:
				changeState(arg0, anli_content, anli_state);
				break;
			case R.id.llyt_tihui_click:
				changeState(arg0, tihui_content, tihui_state);
				break;
			case R.id.llyt_quxue_click:
				changeState(arg0, quxue_content, quxue_state);
				break;
		}
	}

	/**
	 * 控制切换
	 *
	 * @param view
	 *            点击的View
	 * @param ContentView
	 *            显示内容的View
	 * @param stateView
	 *            加减号的TextView
	 */
	private void changeState(View view, View ContentView, TextView stateView) {
		String tag = (String) view.getTag();
		if (tag.equals(CLOSE)) {
			ContentView.setVisibility(View.VISIBLE);
			ContentView.startAnimation(m_Animation_open);
			view.setTag(OPEN);
			stateView.setText("-");
		} else {
			ContentView.startAnimation(m_Animation_close);
			m_Animation_close.setAnimationListener(new CloseAnimationListener(
					ContentView));
			view.setTag(CLOSE);
			stateView.setText("+");
		}
	}

}
